package unlp_fi.Test_LRS;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;

public class ActorTest extends AbstractTest {

	public int createActorTest() {
		try {
			//

			HttpURLConnection conn = super.connectionPOST("actor/createAgent");

			// Actor json
			String actorJson;

			// CREA AGENTE
			// actorJson =
			// "{\"name\": \"Andrew Downes\", \"mbox\": \"mailto:andrew@example.co.uk\",  \"type\": \"AGENT\"}";

			// CREA GRUPO
			actorJson = "{\"name\": \"LAbo de soft\"}";

			OutputStream os = conn.getOutputStream();
			os.write(actorJson.getBytes());
			os.flush();

			if (conn.getResponseCode() < 200 & conn.getResponseCode() >= 300) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);

			}

			conn.disconnect();

		} catch (Exception e) {

			e.printStackTrace();

		}
		return 1;
	}

	public void getActorTest(int id) {

		try {

			HttpURLConnection conn = super.connectionGET("actor/get/" + id);

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			conn.disconnect();

		} catch (Exception e) {

			e.printStackTrace();

		}
	}

	public void asociteAgentToGroup() {

		
		try {
			HttpURLConnection conn = super.connectionGET("actor/addToGroup/"
					+ 4 + "/" + 1);

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			conn.disconnect();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

}
